use serde::{Deserialize, Serialize};

// CTP 账户配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CtpAccountConfig {
    pub broker_id: String,
    pub account: String,
    pub password: String,
    pub trade_front: String,
    pub md_front: String,
    pub auth_code: String,
    pub user_product_info: String,
    pub app_id: String,
}

// API 响应结构
#[derive(Debug, Serialize, Deserialize)]
pub struct ApiResponse<T> {
    pub success: bool,
    pub data: Option<T>,
    pub error: Option<String>,
}

// 行情数据请求
#[derive(Debug, Serialize, Deserialize)]
pub struct MarketDataRequest {
    pub instrument_ids: Vec<String>,
}

// 订单请求
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrderRequest {
    pub instrument_id: String,
    pub direction: String, // "0" for buy, "1" for sell
    pub price: f64,
    pub volume: i32,
    pub order_type: String, // "1" for limit order, "2" for limit price
    pub offset_flag: Option<String>, // "0" for open, "1" for close
    pub hedge_flag: Option<String>, // "1" for speculation, "2" for arbitrage, "3" for hedge
    pub time_condition: Option<String>, // "1" for IOC, "3" for GFD
    pub volume_condition: Option<String>, // "1" for any volume
}

// 窗口上下文信息
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WindowContext {
    #[serde(alias = "windowId")]
    pub window_id: Option<String>,
    #[serde(alias = "contractCode")]
    pub contract_code: Option<String>,
}

impl Default for WindowContext {
    fn default() -> Self {
        Self {
            window_id: None,
            contract_code: None,
        }
    }
}

// 撤单请求
#[derive(Debug, Serialize, Deserialize)]
pub struct CancelOrderRequest {
    pub order_ref: String,
    pub front_id: Option<i32>,
    pub session_id: Option<i32>,
    pub exchange_id: Option<String>,
    pub order_sys_id: Option<String>,
    pub instrument_id: String,
}

// 账户信息
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccountInfo {
    pub broker_id: String,
    pub account_id: String,
    pub pre_mortgage: f64,
    pub pre_credit: f64,
    pub pre_deposit: f64,
    pub pre_balance: f64,
    pub pre_margin: f64,
    pub interest_base: f64,
    pub interest: f64,
    pub deposit: f64,
    pub withdraw: f64,
    pub frozen_margin: f64,
    pub frozen_cash: f64,
    pub frozen_commission: f64,
    pub curr_margin: f64,
    pub cash_in: f64,
    pub commission: f64,
    pub close_profit: f64,
    pub position_profit: f64,
    pub balance: f64,
    pub available: f64,
    pub withdraw_quota: f64,
    pub reserve: f64,
    pub trading_day: String,
    pub settlement_id: i32,
    pub credit: f64,
    pub mortgage: f64,
    pub exchange_margin: f64,
    pub delivery_margin: f64,
    pub exchange_delivery_margin: f64,
    pub reserve_balance: f64,
    pub currency_id: String,
    pub pre_fund_mortgage_in: f64,
    pub pre_fund_mortgage_out: f64,
    pub fund_mortgage_in: f64,
    pub fund_mortgage_out: f64,
    pub fund_mortgage_available: f64,
    pub mortgage_able_fund: f64,
    pub spec_product_margin: f64,
    pub spec_product_frozen_margin: f64,
    pub spec_product_commission: f64,
    pub spec_product_frozen_commission: f64,
    pub spec_product_position_profit: f64,
    pub spec_product_close_profit: f64,
    pub spec_product_position_profit_by_alg: f64,
    pub spec_product_exchange_margin: f64,
    pub bis_margin: f64,
    pub bis_frozen_margin: f64,
    pub bis_commission: f64,
    pub bis_frozen_commission: f64,
    pub bis_position_profit: f64,
    pub bis_close_profit: f64,
    pub bis_position_profit_by_alg: f64,
    pub bis_exchange_margin: f64,
    pub frozen_swap: f64,
    pub remain_swap: f64,
}

// 合约信息
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InstrumentInfo {
    pub instrument_id: String,
    pub exchange_id: String,
    pub instrument_name: String,
    pub exchange_inst_id: String,
    pub product_id: String,
    pub product_class: String,
    pub delivery_year: i32,
    pub delivery_month: i32,
    pub max_market_order_volume: i32,
    pub min_market_order_volume: i32,
    pub max_limit_order_volume: i32,
    pub min_limit_order_volume: i32,
    pub volume_multiple: i32,
    pub price_tick: f64,
    pub create_date: String,
    pub open_date: String,
    pub expire_date: String,
    pub start_deliv_date: String,
    pub end_deliv_date: String,
    pub inst_life_phase: String,
    pub is_trading: i32,
    pub position_type: String,
    pub position_date_type: String,
    pub long_margin_ratio: f64,
    pub short_margin_ratio: f64,
    pub max_margin_side_algorithm: String,
    pub underlying_inst_id: String,
    pub strike_price: f64,
    pub options_type: String,
    pub underlying_multiple: f64,
    pub combination_type: String,
}

// 持仓信息
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PositionInfo {
    pub instrument_id: String,
    pub broker_id: String,
    pub investor_id: String,
    pub posi_direction: String,
    pub hedge_flag: String,
    pub position_date: String,
    pub yd_position: i32,
    pub position: i32,
    pub long_frozen: i32,
    pub short_frozen: i32,
    pub long_frozen_amount: f64,
    pub short_frozen_amount: f64,
    pub open_volume: i32,
    pub close_volume: i32,
    pub open_amount: f64,
    pub close_amount: f64,
    pub position_cost: f64,
    pub pre_margin: f64,
    pub use_margin: f64,
    pub frozen_margin: f64,
    pub frozen_cash: f64,
    pub frozen_commission: f64,
    pub cash_in: f64,
    pub commission: f64,
    pub close_profit: f64,
    pub position_profit: f64,
    pub pre_settlement_price: f64,
    pub settlement_price: f64,
    pub trading_day: String,
    pub settlement_id: i32,
    pub open_cost: f64,
    pub exchange_margin: f64,
    pub comb_position: i32,
    pub comb_long_frozen: i32,
    pub comb_short_frozen: i32,
    pub close_profit_by_date: f64,
    pub close_profit_by_trade: f64,
    pub today_position: i32,
    pub margin_rate_by_money: f64,
    pub margin_rate_by_volume: f64,
    pub strike_frozen: i32,
    pub strike_frozen_amount: f64,
    pub abandon_frozen: i32,
    pub exchange_id: String,
    pub yd_strike_frozen: i32,
    pub invest_unit_id: String,
    pub position_cost_offset: f64,
    pub tas_position: i32,
    pub tas_position_cost: f64,
}

// 结算确认请求
#[derive(Debug, Serialize, Deserialize)]
pub struct SettlementConfirmRequest {
    pub broker_id: String,
    pub investor_id: String,
    pub confirm_date: String,
    pub confirm_time: String,
}

// 订单信息结构
#[derive(Debug, Clone, serde::Serialize)]
pub struct OrderInfo {
    pub order_ref: String,
    pub instrument_id: String,
    pub direction: String,
    pub price: f64,
    pub volume: i32,
    pub volume_traded: i32,
    pub order_status: String,
    pub front_id: i32,
    pub session_id: i32,
}

